using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using OrderSystem.SystemFramework;
using System;
using System.Data;
using System.Diagnostics;
using System.IO;

namespace OrderSystem.NPOIOperateExcel
{
	public class ExportProductLabelData
	{
		private HSSFWorkbook hssfworkbook;

		private FileStream fs;

		private bool OpenFileAfterSave;

		private bool _isDisposed = false;

		public ExportProductLabelData(string templetFile, bool bOpenFileAfterSave)
		{
			FileStream fileStream = File.Open(templetFile, FileMode.Open);
			this.hssfworkbook = new HSSFWorkbook(fileStream);
			this.OpenFileAfterSave = bOpenFileAfterSave;
			fileStream.Close();
			fileStream.Dispose();
		}

		public void MakeDataToExcel(string sSavePath, DataTable dt)
		{
			string defaultVersionOfOutType = AppSettings.GetDefaultVersionOfOutType("ExportProductLabelData");
			if (defaultVersionOfOutType == "1")
			{
				this.MakeDataToExcel_1(sSavePath, dt);
			}
		}

		public void MakeDataToExcel_1(string sSavePath, DataTable dt)
		{
			ISheet sheetAt = this.hssfworkbook.GetSheetAt(0);
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				IRow row = sheetAt.CreateRow(i + 1);
				row.CreateCell(0).SetCellValue((dt.Rows[i]["标签上的客户名称"].ToString() == "") ? dt.Rows[i]["计划名称"].ToString() : dt.Rows[i]["标签上的客户名称"].ToString());
				row.CreateCell(1).SetCellValue(dt.Rows[i]["产品编号"].ToString());
				row.CreateCell(2).SetCellValue(dt.Rows[i]["产品类型"].ToString());
				row.CreateCell(3).SetCellValue(dt.Rows[i]["宽"].ToString());
				row.CreateCell(4).SetCellValue(dt.Rows[i]["高"].ToString());
				row.CreateCell(5).SetCellValue(dt.Rows[i]["产品数量"].ToString());
				row.CreateCell(6).SetCellValue(dt.Rows[i]["产品备注"].ToString() + ((dt.Rows[i]["产品备注"].ToString() == "" || dt.Rows[i]["产品工艺"].ToString() == "") ? "" : " / ") + dt.Rows[i]["产品工艺"].ToString());
				row.CreateCell(7).SetCellValue(dt.Rows[i]["产品ID"].ToString().Substring(4));
				row.CreateCell(9).SetCellValue("0");
				string cellValue = string.Concat(new string[]
				{
					dt.Rows[i]["宽"].ToString(),
					(dt.Rows[i]["宽(短边)"].ToString() == "") ? "" : ("/" + dt.Rows[i]["宽(短边)"].ToString()),
					" × ",
					dt.Rows[i]["高"].ToString(),
					(dt.Rows[i]["高(短边)"].ToString() == "") ? "" : ("/" + dt.Rows[i]["高(短边)"].ToString())
				});
				if (dt.Rows[i]["加工要求"].ToString().Length > 0)
				{
					cellValue = dt.Rows[i]["加工要求"].ToString();
				}
				row.CreateCell(10).SetCellValue(cellValue);
				row.CreateCell(11).SetCellValue(dt.Rows[i]["订单备注"].ToString());
			}
			using (this.fs = File.OpenWrite(sSavePath))
			{
				this.hssfworkbook.Write(this.fs);
				this.fs.Close();
				this.fs.Dispose();
				this.hssfworkbook.Clear();
				if (this.OpenFileAfterSave)
				{
					Process.Start(sSavePath);
				}
			}
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (!this._isDisposed)
			{
				if (disposing)
				{
					if (this.hssfworkbook != null)
					{
						this.hssfworkbook.Clear();
					}
					if (this.fs != null)
					{
						this.fs.Close();
						this.fs.Dispose();
					}
				}
				this._isDisposed = true;
			}
		}

		~ExportProductLabelData()
		{
			this.Dispose(false);
		}
	}
}
